<?php

class Ext_Oauth
{
    protected static $_instance = null;
    protected $_storage = null;
    
    private $_adapter = null;
    private $_oauthResult = null;
    private $_userId = null;
    
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
    
    public function __construct($adapter)
    {
        $this->_adapter = $adapter;
        
        $oauthResult = $this->getStorage()->setMember('oauthResult')->read();
        $this->_oauthResult = unserialize($oauthResult);
    }
    
    public function setBind($userId)
    {
        if(!isset($this->_oauthResult)){
            return "";
        }
        if(!($this->_oauthResult instanceof Ext_Oauth_Result)){
            return "";
        }
        
        $this->getStorage()->setMember('oauth')->clear();
        $userAccountOAuthRow = App_Model_DbTable_User_Account_OAuth::getInstance()->getRowByIdentitySite($this->_oauthResult->getIdentity(), $this->_oauthResult->getSite());
        if(!isset($userAccountOAuthRow['userId']) || $userAccountOAuthRow['userId'] < 1){
            if(isset($userAccountOAuthRow['id'])){
                $row = array(
                    'userId'      => $userId,
                );
                App_Model_DbTable_User_Account_OAuth::getInstance()->addOld($row, $_userAccountOAuthRow['id']);
            }else {
                $row = array(
                    'identity'     => $this->_oauthResult->getIdentity(),
                    'site'         => $this->_oauthResult->getSite(),
                    'userId'       => $userId,
                );
                App_Model_DbTable_User_Account_OAuth::getInstance()->addNew($row);
            }
        }else {
            $userId = $userAccountOAuthRow['userId'];
        }
        
        $this->_userId = $userId;
    }
    
    public function hasBind()
    {
        if($this->getBind() > 0){
            return true;
        }
        return false;
    }
    
    public function getBind()
    {
        if(isset($this->_userId))
        {
            return $this->_userId;
        }
        
        if(!isset($this->_oauthResult))
        {
            return "";
        }

        $userAccountOAuthRow = App_Model_DbTable_User_Account_OAuth::getInstance()->getRowByIdentitySite($this->_oauthResult->getIdentity(), $this->_oauthResult->getSite());
        if(isset($userAccountOAuthRow['userId'])){
            $this->getStorage()->setMember('oauth')->clear();
            $this->_userId = $userAccountOAuthRow['userId'];
        }
        
        return $this->_userId;
    }
    
    public function hasAuthenticate()
    {
        if(!isset($this->_oauthResult)){
            return false;
        }
        if(!($this->_oauthResult instanceof Ext_Oauth_Authenticate_Result)){
            return false;
        }
        if($this->_oauthResult->getDataSite() != $this->_adapter){
            return false;
        }
        
        return true;
    }
    
    public function doOauth()
    {
        $oauthAdapter = new Ext_Oauth_Adapter($this->_adapter);
        $oauthResult = $oauthAdapter->authenticate();
        $this->getStorage()->setMember('oauthResult')->write(serialize($oauthResult));
        
        $this->_oauthResult = $oauthResult;
                
        return $this;
    }
    
    public function getStorage()
    {
        if (null === $this->_storage) {
            require_once 'Ext/Session.php';
            $this->setStorage(new Ext_Session());
        }

        return $this->_storage;
    }

    public function setStorage(Ext_Session $storage)
    {
        $this->_storage = $storage;
        return $this;
    }
}
